{

    // 你的图片地址，可替换为自己的
    const images = [
        './imgs/nd1.png',
        './imgs/nd2.png',
        './imgs/nd3.png',
    ];

    const carousel = document.getElementById('carousel2');
    const track = document.getElementById('carouselTrack2');
    const indicators = document.getElementById('carouselIndicators2');
    const slideCount = images.length;
    let currentIndex = 1; // 从1开始，因前后各有一张克隆
    let timer = null;
    let isTransitioning = false;

    // 初始化slides（首尾克隆）
    function initSlides() {
        track.innerHTML = '';
        const slides = [];
        // 克隆最后一张到最前面
        slides.push(`<div class="carousel-slide"><img src="${images[slideCount - 1]}" alt=""></div>`);
        // 原始图片
        for (let img of images) {
            slides.push(`<div class="carousel-slide"><img src="${img}" alt=""></div>`);
        }
        // 克隆第一张到最后面
        slides.push(`<div class="carousel-slide"><img src="${images[0]}" alt=""></div>`);
        track.innerHTML = slides.join('');
        track.style.transform = `translateX(-${currentIndex * 100}%)`;
    }

    // 初始化指示点
    function initIndicators() {
        indicators.innerHTML = '';
        for (let i = 0; i < slideCount; i++) {
            const btn = document.createElement('button');
            btn.className = 'carousel-indicator' + (i === 0 ? ' active' : '');
            btn.setAttribute('data-index', i);
            btn.addEventListener('click', () => {
                goToSlide(i + 1, true);
            });
            indicators.appendChild(btn);
        }
    }

    // 切换到指定slide
    function goToSlide(index, stopAuto = false) {
        if (isTransitioning) return;
        isTransitioning = true;
        currentIndex = index;
        track.style.transition = 'transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0)';
        track.style.transform = `translateX(-${currentIndex * 100}%)`;
        updateIndicators();
        if (stopAuto) resetAutoPlay();
    }

    // 轮播到下一张
    function nextSlide() {
        if (isTransitioning) return;
        goToSlide(currentIndex + 1);
    }

    // 轮播到上一张
    function prevSlide() {
        if (isTransitioning) return;
        goToSlide(currentIndex - 1);
    }

    // 更新指示点
    function updateIndicators() {
        const idx = (currentIndex - 1 + slideCount) % slideCount;
        [...indicators.children].forEach((btn, i) => {
            btn.classList.toggle('active', i === idx);
        });
    }

    // 处理无缝切换
    track.addEventListener('transitionend', () => {
        isTransitioning = false;
        if (currentIndex === 0) {
            // 跳到倒数第二张（视觉上是最后一张）
            track.style.transition = 'none';
            currentIndex = slideCount;
            track.style.transform = `translateX(-${currentIndex * 100}%)`;
        } else if (currentIndex === slideCount + 1) {
            // 跳到第一张
            track.style.transition = 'none';
            currentIndex = 1;
            track.style.transform = `translateX(-${currentIndex * 100}%)`;
        }
    });

    // 自动播放
    function startAutoPlay() {
        timer = setInterval(nextSlide, 2800);
    }
    function stopAutoPlay() {
        clearInterval(timer);
    }
    function resetAutoPlay() {
        stopAutoPlay();
        startAutoPlay();
    }

    // 鼠标悬浮/离开停止/启动自动播放
    carousel.addEventListener('mouseenter', stopAutoPlay);
    carousel.addEventListener('mouseleave', startAutoPlay);

    // 支持触摸滑动
    let startX = null, moveX = 0;
    carousel.addEventListener('touchstart', e => {
        stopAutoPlay();
        startX = e.touches[0].clientX;
        moveX = 0;
        track.style.transition = 'none';
    });
    carousel.addEventListener('touchmove', e => {
        if (startX === null) return;
        moveX = e.touches[0].clientX - startX;
        track.style.transform = `translateX(-${currentIndex * 100 + (-moveX / carousel.offsetWidth) * 100}%)`;
    });
    carousel.addEventListener('touchend', () => {
        track.style.transition = 'transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0)';
        if (Math.abs(moveX) > carousel.offsetWidth / 4) {
            if (moveX < 0) nextSlide();
            else prevSlide();
        } else {
            track.style.transform = `translateX(-${currentIndex * 100}%)`;
        }
        startX = null;
        moveX = 0;
        resetAutoPlay();
    });

    // 初始化
    initSlides();
    initIndicators();
    startAutoPlay();

    // 窗口缩放自适应
    window.addEventListener('resize', () => {
        track.style.transition = 'none';
        track.style.transform = `translateX(-${currentIndex * 100}%)`;
    });

    // 给商品加上点击事件
    const doms = document.querySelectorAll('.grid-item');
    for (let i = 0; i < doms.length; i++) {
        doms[i].addEventListener('click', () => {
            window.location = 'buy.html';
        });
    }

    // 更多添加事件
    const moreDoms = document.querySelectorAll('.more');
    for (let i = 0; i < moreDoms.length; i++) {
        moreDoms[i].addEventListener('click', () => {
            window.location = 'list.html';
        });
    }

    // 当前滚动下标
    let curIndex = 0;
    // 最多滚动次数
    let scrollTimes = 2;

    // 获取子元素个数
    const target = document.getElementById('carouselList');
    const childrenCount = target.children.length;

    scrollTimes = Math.ceil(childrenCount / 3);

    // 二手好物区点击滚动
    const prevBtn = document.getElementById('prevBtn');
    const nextBtn = document.getElementById('nextBtn');

    function scrollByOne(direction) {
        // 获取滚动容器
        const scrollDom = document.getElementById('carouselList');
        if (direction === 'left') {
            curIndex -= 1;
            if (curIndex < 0) {
                curIndex = 0;
                return;
            }
            scrollDom.style.transform = `translateX(${curIndex * (-34)}%)`;
        } else if (direction === 'right') {
            curIndex += 1;
            if (curIndex >= scrollTimes) {
                curIndex = scrollTimes - 1;
                return;
            }
            scrollDom.style.transform = 'translateX(-' + (curIndex * 34) + '%)';
        }
    }

    prevBtn.onclick = () => {
        scrollByOne('left');
    };
    nextBtn.onclick = () => {
        scrollByOne('right');
    };

    const buyDoms = document.querySelectorAll('.carousel-buy');
    for (let i = 0; i < buyDoms.length; i++) {
        buyDoms[i].addEventListener('click', () => {
            window.location = 'list.html';
        });
    }
}